package com.dshore.messagecenter.config;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.dshore.messagecenter.dao.system.SysCustomProcessDao;
import com.dshore.messagecenter.util.secret.AESGCMUpdateAAD2;

@Configuration
@Component
public class DataSourcceConfig {
	
	@Value("${spring.rabbitmq.host}")
	private String host;
	@Value("${spring.rabbitmq.port}")
	private Integer port;
	@Value("${spring.rabbitmq.virtual-host}")
	private String vhost;
	@Value("${spring.rabbitmq.username}")
	private String username;
	@Value("${spring.rabbitmq.secret}")
	private String rabbitmqSecret;
	
	@Autowired
	private SysCustomProcessDao SysCustomProcessDao;
	
	@Bean
	public CachingConnectionFactory getConnectionFactory() {
		CachingConnectionFactory factory = new CachingConnectionFactory();
		factory.setHost(host);
		factory.setPort(port);
		factory.setVirtualHost(vhost);
		factory.setUsername(username);
		try {
			String publicKey = SysCustomProcessDao.selectPublicKey();
			factory.setPassword(AESGCMUpdateAAD2.decript(publicKey, rabbitmqSecret));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return factory;
	}
}
